package com.yunhe.sms.controller;

import com.yunhe.commens.dto.Pagedao;
import com.yunhe.sms.entity.system.Premission;
import com.yunhe.sms.entity.system.Role;
import com.yunhe.sms.entity.system.User;
import com.yunhe.sms.service.Loginservice;
import com.yunhe.sms.service.system.PremissionService;
import com.yunhe.sms.service.system.RoleService;
import com.yunhe.sms.service.system.impl.PremissionServiceImpl;
import com.yunhe.sms.service.system.impl.RoleServiceImpl;
import com.yunhe.webmvc.annocation.RequestBean;
import com.yunhe.webmvc.annocation.RequestMapping;
import com.yunhe.webmvc.annocation.ResponseJSON;
import com.yunhe.webmvc.mode.JsonResponce;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author LeiPeifeng
 * @version V1.0
 * @date 2017/10/12 0012 15:27
 * @Description:
 */
@RequestMapping("/")
public class IndexController {
    private PremissionService premissionService=new PremissionServiceImpl ();


    @RequestMapping("index")
    public  String index(){
        return "index";
    }
    @RequestMapping("list")
    public  String list(HttpServletRequest req)  {
        List<Premission> findlist = premissionService.findList ();
        req.setAttribute ( "premissionList",findlist );
        return "system/module/list";
    }

    @RequestMapping("add")
    public String add(){
        return "system/module/add";
    }

    @RequestMapping("edit")
    public String edit(HttpServletRequest req,Long id){
        Premission premission = premissionService.find ( id );
        if (null != premission && premission.getType ()>1){
            List<Premission> type = premissionService.findType ( premission.getType () == 2 ? 1 : 2 );
            req.setAttribute ( "premiType",type );
        }
        req.setAttribute ( "premission",premission );
        return "system/module/edit";
    }
    @RequestMapping("update")
    public String update(@RequestBean Premission premission){
        premissionService.update ( premission );
        return "redirect:list";
    }

    @RequestMapping("deleted")
    @ResponseJSON
    public JsonResponce deleted(String ids ){
        premissionService.deleted ( ids );
        return new JsonResponce();
    }

    @RequestMapping("save")
    public String sava(@RequestBean Premission premission){
        premissionService.save ( premission );
        return "redirect:list";
    }

    @RequestMapping("type")
    @ResponseJSON
    public List<Premission> typeList(int type,HttpServletResponse resp){
        resp.setCharacterEncoding ( "utf-8" );
        List<Premission> type1;
        if (type==3||type==4){
            type1 = premissionService.findTypeList ( "type>2" );
        }else{
            type1 = premissionService.findType ( type );
        }
        return type1;
    }

    @RequestMapping("page")
    @ResponseJSON
    public Pagedao<Premission> page(int offset,int limit,HttpServletResponse resp){
        resp.setCharacterEncoding ( "utf-8" );
        Pagedao<Premission> page = premissionService.page ( offset, limit );
        return page;
    }

    @RequestMapping("parentId")
    @ResponseJSON
    public List<Premission> parentId(int parentsId,HttpServletResponse resp){
        resp.setCharacterEncoding ( "utf-8" );
        List<Premission> parent = premissionService.findParent ( parentsId );
        return parent;
    }


}
